package com.yl.plugin;

import com.yl.plugin.util.Util;
import org.mybatis.generator.api.CommentGenerator;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.internal.util.StringUtility;

import java.util.Properties;

/**
 * 自定义实现代码生成时，包含中文注释
 * <pre>
 *  mybatis-generator 默认注释实现类 {@link  org.mybatis.generator.internal.DefaultCommentGenerator }
 *  生成英文注释，不美观，并且不是表字段注释
 *  配置方式
 *      &lt;commentGenerator type="com.yl.ext.ZhCnCommentGenerator"&gt;
 *         &lt;property name="suppressDate" value="false" /&gt;
 *         &lt;property name="suppressAllComments" value="false" /&gt;
 *       &lt;/commentGenerator&gt;
 * </pre>
 *
 * @author liuxubo
 * @date 2023/7/5 11:44
 */
public class ZhCnCommentGenerator implements CommentGenerator {
    /**
     * 控制类上注释中是否生成日期
     */
    private boolean suppressDate;
    private boolean suppressAllComments;


    public ZhCnCommentGenerator() {
        super();
    }

    @Override
    public void addConfigurationProperties(Properties properties) {
        this.suppressDate = StringUtility.isTrue(properties.getProperty("suppressDate"));
        this.suppressAllComments = StringUtility.isTrue(properties.getProperty("suppressAllComments"));

    }

    /**
     * 生成的实体类上添加注释
     *
     * @param topLevelClass
     * @param introspectedTable
     */
    @Override
    public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        if (this.suppressAllComments) {
            return;
        }

        Util.setClassComment(introspectedTable.getRemarks(), introspectedTable.getContext()).forEach(s -> topLevelClass.addJavaDocLine(s));
    }

    @Override
    public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
        if (this.suppressAllComments) {
            return;
        }

        Util.setFieldComment(introspectedColumn.getRemarks()).forEach(s -> field.addJavaDocLine(s));
    }

    @Override
    public void addFieldComment(Field field, IntrospectedTable introspectedTable) {
        if (this.suppressAllComments) {
            return;
        }

        Util.setFieldComment(introspectedTable.getRemarks()).forEach(s -> field.addJavaDocLine(s));
    }


    @Override
    public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean markAsDoNotDelete) {
        if (this.suppressAllComments) {
            return;
        }

        Util.setClassComment(introspectedTable.getRemarks(), introspectedTable.getContext()).forEach(s -> innerClass.addJavaDocLine(s));
    }

    @Override
    public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) {
        if (this.suppressAllComments) {
            return;
        }

        Util.setClassComment(introspectedTable.getRemarks(), introspectedTable.getContext()).forEach(s -> innerClass.addJavaDocLine(s));
    }

    public void addEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable) {
        if (this.suppressAllComments) {
            return;
        }

        Util.setFieldComment(introspectedTable.getFullyQualifiedTable().toString()).forEach(s -> innerEnum.addJavaDocLine(s));
    }

    @Override
    public void addGetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
        if (this.suppressAllComments) {
            return;
        }
        //getter方法不添加注释
    }


    @Override
    public void addSetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
        if (this.suppressAllComments) {
            return;
        }
        //setter方法不添加注释
    }


}